package com.masterclient.app.businessmodule.homemodule.view.adapter.template;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselibrary.utils.GlideUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.homecms.HomeNewCmsIndexBean;
import com.masterclient.app.businessmodule.homemodule.bean.homecms.HomeNewCmsItemBean;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselibrary.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ConcentrationProductItemAdapter extends RecyclerView.Adapter {


    private Context context;

    private int convert_flag;
    private List<HomeNewCmsItemBean> shProducts;

    private HomeNewCmsIndexBean shCollect;
    private String activity_title;
    private String activity_id;
    private String source;


    public ConcentrationProductItemAdapter(Context context, List<HomeNewCmsItemBean> shProducts
            , HomeNewCmsIndexBean shCollect, String title, String source, String activity_id) {
        this.context = context;
        this.shProducts = shProducts;
        this.shCollect = shCollect;
        this.activity_title = title;
        this.source = source;
        this.activity_id = activity_id;
    }

    @NonNull
    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_concentration_product_item_item,parent,false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,  int position) {


        ViewHolder viewHolder = (ViewHolder) holder;
        if (position == 0){
            viewHolder.viewLeftGap.setVisibility(View.VISIBLE);
        }else {
            viewHolder.viewLeftGap.setVisibility(View.GONE);
        }
        if(position == 4 && shProducts.size() > 5){
            viewHolder.more.setVisibility(View.VISIBLE);
            viewHolder.line.setVisibility(View.VISIBLE);
        }else{
            viewHolder.more.setVisibility(View.GONE);
            viewHolder.line.setVisibility(View.GONE);
        }

        HomeNewCmsItemBean shProduct = shProducts.get(position);

        GlideUtil.displayImage(context,shProduct.getImage(),viewHolder.image,R.mipmap.defaults);
        viewHolder.title.setText(shProduct.getTitle());

        Utils.set_price_style2(viewHolder.price,"¥ " + shProduct.getShow_price());
        Utils.setDin(viewHolder.price,context);
        viewHolder.tvOfficialPrice.setText("¥" +shProduct.getLine_price());
        viewHolder.tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        viewHolder.tvOfficialPrice.setVisibility(View.VISIBLE);

        viewHolder.product_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewCommodityDetailActivity.class);
                intent.putExtra("id",shProduct.getProduct_id());
                intent.putExtra("source_page", "首页");
                intent.putExtra("source_module", "精选活动");
                intent.putExtra("source_position", position+"");
                intent.putExtra("product_sourcepage", activity_title);
                intent.putExtra("source_page_id", activity_id);
                context.startActivity(intent);
            }
        });

        viewHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    CommonUtil.goAnyWhere(context, shCollect.getUrl(), "", "", "", "",source,"精选活动");
                }catch (Exception e){
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return shProducts.size() > 5 ? 5 : shProducts.size();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item)
        public View item;
        @BindView(R.id.view_left_gap)
        public View viewLeftGap;
        @BindView(R.id.more)
        public View more;
        @BindView(R.id.line)
        public View line;
        @BindView(R.id.product_layout)
        public View product_layout;
        @BindView(R.id.image)
        public ImageView image;
        @BindView(R.id.title)
        public TextView title;
        @BindView(R.id.price)
        public TextView price;
        @BindView(R.id.tv_official_price)
        public TextView tvOfficialPrice;

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this,view);
        }
    }


}

